package cn.jimmie.learn.nattyfragmentplugin;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.lang.reflect.Field;
import java.util.List;

import cn.jimmie.learn.sdk.OnSdkLoginListener;
import cn.jimmie.learn.sdk.SdkCenter;
import dalvik.system.BaseDexClassLoader;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn_6 = findViewById(R.id.btn_6);
        SdkCenter.init(this);

        //带资源文件的调用
        btn_6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                SdkCenter.login(MainActivity.this, new OnSdkLoginListener() {
                    @Override
                    public void onLoginFinished(boolean success, String message) {
                        Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        hookNativePaths();
    }

    private static void hookNativePaths() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            for (String supportedAbi : Build.SUPPORTED_ABIS) {
                System.out.println("支持的cpu型号 :" + supportedAbi);
            }
        }
        try {
            Object classLoader = Thread.currentThread().getContextClassLoader();
            Class<BaseDexClassLoader> BaseDexklass = BaseDexClassLoader.class;
            Field pathListField = BaseDexklass.getDeclaredField("pathList");
            pathListField.setAccessible(true);

            Object pathList = pathListField.get(classLoader);
            Class<?> dexPathKlass = Class.forName("dalvik.system.DexPathList");
            Field nativeLibsField = dexPathKlass.getDeclaredField("nativeLibraryDirectories");
            nativeLibsField.setAccessible(true);

            List<File> nativeLibs = (List<File>) nativeLibsField.get(pathList);

            for (File nativeLib : nativeLibs) {
                System.out.println("so包的系统路径 : " + nativeLib.getAbsoluteFile());
            }
        } catch (Exception e) {
            System.out.println("error");
            e.printStackTrace();
        }
    }
}
